// Copyright (C) 2008 Taylor L. Riche <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

/**
 * 
 */
package syscomp.entities;

import core.CodeGenerator;
import core.Entity;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author riche
 *
 */
public class MemGroup extends Entity {
	
	private HashMap<String, ProcessingElement> procMap;

	private String name;
	
	/**
	 * @param name
	 */
	private MemGroup(String name, ArrayList<String> procList) {
		this.name = name;
		procMap = new HashMap<String, ProcessingElement>();
		for(String s : procList) {
			procMap.put(s, null);
		}
	}

	static public MemGroup create(String name, ArrayList<String> procList) {
		return new MemGroup(name, procList);
	}
	
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	public void resolve(CodeGenerator cg) {
		for(String s : procMap.keySet()) {
			ProcessingElement value = (ProcessingElement)cg.lookupName(s);
			procMap.put(s, value);
		}
	}
	
	public boolean isInGroup(String procName) {
		return procMap.containsKey(procName);
	}
	
	public ProcessingElement getProcByName(String procName) {
		return procMap.get(procName);
	}
	
	public ArrayList<ProcessingElement> getProcList() {
		ArrayList<ProcessingElement> ret = new ArrayList<ProcessingElement>();
		for(ProcessingElement p : procMap.values()) {
			ret.add(p);
		}
		return ret;
	}
	
	public void generateFrameworkCode(PrintStream file) {
		for (ProcessingElement p : this.procMap.values()) {
			file.println("\t\ttempMG->insertProcessor(procMap[\"" + p.getName() + "\"]);");
		}
	}

	/* (non-Javadoc)
	 * @see core.Entity#generateCode(core.CodeGenerator)
	 */
	@Override
	public void generateCode(CodeGenerator cg) {
		/// UNUSED!
		String filename = "lc_" + this.getName() + "_MemGroup.h";
		FileOutputStream out = null;
		PrintStream file = null;
		try {
		    out = new FileOutputStream(filename);
		    file = new PrintStream(out);
		}
		catch (FileNotFoundException e) {
		    System.err.println("Specified file " + filename + " does not exist.");
		    System.exit(1);
		}

		// Print guards
		file.println("#ifndef LC_" + this.getName().toUpperCase() + "_H_");
		file.println("#define LC_" + this.getName().toUpperCase() + "_H_");
		file.println();
		
		file.println("#include <lagniappe/MemGroup.h>");
		file.println("#include <lagniappe/ProcElement.h>");
		

		file.println("class " + this.getName() + " : public lagniappe::MemGroup");		
		
		// Close guards
		file.println("#endif");
		file.println();

	}

}
